package cst.start.teachresourceplatform224xmz.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cst.start.teachresourceplatform224xmz.R;
import cst.start.teachresourceplatform224xmz.bean.LoginBean;
import cst.start.teachresourceplatform224xmz.iface.AmicoolListener;
import cst.start.teachresourceplatform224xmz.model.AmicoolModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username;
    private EditText password;
    private Button login_button;
    private SharedPreferences sp;
    private TextView register;
    private CheckBox checkBox;
    private AmicoolModel amicoolmodel;
    private AmicoolListener<LoginBean> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        listener = new AmicoolListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                //5
                if (l.getSessionid()!=null){
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this,MainActivity.class);
                    SharedPreferences.Editor  editor=sp.edit();
                    boolean isremember = checkBox.isChecked();
                    if (isremember){
                        editor.putString("name",username.getText().toString());
                        editor.putString("pass",password.getText().toString());
                        editor.putBoolean("remember",isremember);
                    }
                    editor.putString("sessionid",l.getSessionid().toString());
                    editor.commit();
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this, "用户或密码错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg, Toast.LENGTH_SHORT).show();
            }
        };
    }

    private void init() {
        username = (EditText)findViewById(R.id.et_username);
        password = (EditText)findViewById(R.id.et_password);
        login_button = (Button) findViewById(R.id.btn_login);
        register = (TextView)findViewById(R.id.btn_register);
        checkBox = (CheckBox)findViewById(R.id.checkbox_remember);
        sp = getSharedPreferences("login",MODE_PRIVATE);
        register.setOnClickListener(this);
        login_button.setOnClickListener(this);
        Boolean isremember=sp.getBoolean("remember",false);
        if (isremember){
            checkBox.setChecked(true);
        //2
            username.setText(sp.getString("name",null));
            password.setText(sp.getString("pass",null));
        //
        }

    }
//1
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                amicoolmodel = new AmicoolModel("gson");
                amicoolmodel.login(username.getText().toString(),password.getText().toString(),listener);
                break;
            case R.id.btn_register:
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivity(intent);
        }

    }
}
